jsx3.require("jsx3.gui.Block");

jsx3.Class.defineClass(
		'com.gix.gui.PaginationControler',
		jsx3.gui.Block,
		null,
		function(controler, Block_prototype) {

			/**
			 *  initialize this page control object
			 *  called by itself after it loaded
			 */
			controler.prototype.initialize = function() {
				if (jsx3.ide) {
					return;
				}

				var me = this;
				this.ORDER_INCREASE = 0;
				this.ORDER_DECREASE = 1;

				// get the "lblTotPageNo" handler
				this.totalPageNoLbl = this.getChild(2).getChild(0);

				// get the "txtPageNoInput" handler
				this.txtCurPageNo = this.getChild(1);
				this.txtCurPageNo.doJump = function() {
					me.doJump();
				}

				// get the "imgArrowPrevious" handler
				this.decreaseBtn = this.getChild(0).getChild(0);
				this.decreaseBtn.doChange = function() {
					me.doChange(me.ORDER_DECREASE);
				}

				// get the "imgArrowNext" handler
				this.increaseBtn = this.getChild(2).getChild(1);
				this.increaseBtn.doChange = function() {
					me.doChange(me.ORDER_INCREASE);
				}

				this.totalPageNumber = null;
				this.currentPageNumber = null;
			}

			/**
			 *  setup the current page number and the total page number
			 */
			controler.prototype.setValue = function(currentPageNo, totalPageNo){
				if (jsx3.ide) {
					return;
				}

				this.currentPageNumber = currentPageNo;
				this.txtCurPageNo.setValue(this.currentPageNumber, true);

				this.totalPageNumber = totalPageNo;
				this.totalPageNoLbl.setText(tibco.admin.Main.getServer().getDynamicProperty("@spm_addin@lblTotPageNoText", totalPageNo), true);

				// redraw arrows status
				this.drawArrows();
			}


			/**
			 *  bind the controler with a certain matrix and set the callback target and callback function
			 */
			controler.prototype.bindWithMatrix = function(callbackTarget, callbackFunction, callbackMatrix) {
				if (callbackTarget && callbackFunction) {
					if (typeof(callbackTarget) != "object") {
						throw new tibco.admin.exception.InvalidTargetException("Specified callback target is not an Object");
					}
					else if (typeof(callbackFunction) != "function") {
						throw new tibco.admin.exception.InvalidFunctionException("Specified callback function is not a function");
					}
				}

				this.callbackTarget = callbackTarget;
				this.callbackFunction = callbackFunction;
				this.callbackMatrix = callbackMatrix;
			}

			/**
			 *  jump to the number which is inputed in the field
			 */
			controler.prototype.doChange = function(orderFlag) {
				// call the binding function to change the page content
				if(orderFlag == this.ORDER_INCREASE && this.currentPageNumber < this.totalPageNumber) {
					this.currentPageNumber++;
					// get matrix current page data
					this.getCurrentMtxData();
				}
				if (orderFlag == this.ORDER_DECREASE && this.currentPageNumber > 1)
				{
					this.currentPageNumber--;
					// get matrix current page data
					this.getCurrentMtxData();
				}

				// set back the change number
				if (this.txtCurPageNo.getValue() != this.currentPageNumber) {
					this.txtCurPageNo.setValue(this.currentPageNumber, true);
				}

				// redraw the buttons' status
				this.drawArrows();
			}

			/**
			 *  jump to the number which is inputed in the field
			 */
			controler.prototype.doJump = function() {
				// get the jump number, limit it, and set back it
				this.currentPageNumber = this.txtCurPageNo.getValue();
				if(this.currentPageNumber > this.totalPageNumber) {
					this.currentPageNumber = this.totalPageNumber;
				}
				if(this.currentPageNumber < 1) {
					this.currentPageNumber = 1;
				}
				if (this.txtCurPageNo.getValue() != this.currentPageNumber) {
					this.txtCurPageNo.setValue(this.currentPageNumber, true);
				}

				// get matrix current page data
				this.getCurrentMtxData();
				// redraw the buttons' status
				this.drawArrows();
			}

			/**
			 *  adjust the arrows status by the current page number and the total page number
			 */
			controler.prototype.drawArrows = function() {
				if(this.currentPageNumber <= 1) {
					this.decreaseBtn.setVisibility(jsx3.gui.Block.VISIBILITYHIDDEN, true);
				}
				
				if(this.currentPageNumber >= this.totalPageNumber) {
					this.increaseBtn.setVisibility(jsx3.gui.Block.VISIBILITYHIDDEN, true);
				}
				
				if(this.currentPageNumber > 1 && this.currentPageNumber < this.totalPageNumber) {
					this.decreaseBtn.setVisibility(jsx3.gui.Block.VISIBILITYVISIBLE, true);
					this.increaseBtn.setVisibility(jsx3.gui.Block.VISIBILITYVISIBLE, true);
				}
			}

			/**
			 *  invoke the binded matrix object to do callback function
			 */
			controler.prototype.getCurrentMtxData = function() {
				var msg = new Object();
				msg.mtx = this.callbackMatrix;
				msg.currentPageNumber = this.currentPageNumber;
				this.callbackFunction.call(this.callbackTarget, msg);
			}
		});
